package com.example.qihuibao.widget.view

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import com.example.qihuibao.R

class TextViewWithText(ctx: Context, attrs: AttributeSet? = null) : LinearLayout(ctx, attrs) {
    private var tvName:TextView? = null
    private var tvContent:TextView? = null
    private var tvContentClick:(()->Unit)? = null

    init {
        val root = LayoutInflater.from(ctx).inflate(R.layout.textview_text, this, true)
        tvName = root.findViewById(R.id.tv_name)
        tvContent = root.findViewById(R.id.tv_content)
        tvContent?.setOnClickListener { tvContentClick?.invoke() }
        //
        val typeArray = context.obtainStyledAttributes(attrs, R.styleable.TextViewWithText)
        tvName?.text = typeArray.getString(R.styleable.TextViewWithText_tvname)
        tvContent?.text = typeArray.getString(R.styleable.TextViewWithText_tvcontent)
        tvContent?.isEnabled = typeArray.getBoolean(R.styleable.TextViewWithText_tv_enable, true)
    }

    fun setText(content:String, tvContentClick:()->Unit) {
//        tvName?.text = name
        tvContent?.text = content
        this.tvContentClick = tvContentClick
    }

    fun setContent(content:String) {
        tvContent?.text = content
    }

    fun getContent(): String {
        val s = tvContent?.text.toString()
        return if (s == "点击选择") "" else s
    }

    fun enableContent(enable:Boolean) {
        tvContent?.isEnabled = enable
    }

}